{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-28T07:16:01.302149Z",
     "start_time": "2025-03-28T07:16:01.285047Z"
    }
   },
   "source": [
    "import torch\n",
    "from torchvision import datasets, transforms\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "import torch.optim as optim"
   ],
   "outputs": [],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-28T06:38:11.374423Z",
     "start_time": "2025-03-28T06:38:11.364539Z"
    }
   },
   "cell_type": "code",
   "source": "print(\"PyTorch Version: \", torch.__version__)",
   "id": "5377fa30ac380900",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PyTorch Version:  2.6.0+cu118\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-28T06:38:13.307843Z",
     "start_time": "2025-03-28T06:38:13.197865Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 检测cuda是否可用\n",
    "use_cuda = torch.cuda.is_available()\n",
    "print(\"use_cuda:\", use_cuda)"
   ],
   "id": "76a4f6180080a4b8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "use_cuda: True\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-28T07:16:08.944235Z",
     "start_time": "2025-03-28T07:16:08.934342Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 设置device变量\n",
    "if use_cuda:\n",
    "    device = torch.device(\"cuda\")\n",
    "else:\n",
    "    device = torch.device(\"cpu\")"
   ],
   "id": "50601bf50b64d090",
   "outputs": [],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-28T07:16:11.207682Z",
     "start_time": "2025-03-28T07:16:11.195356Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 设置对数据处理的逻辑\n",
    "transform = transforms.Compose([\n",
    "    # 将图像转化为Tensor张量\n",
    "    transforms.ToTensor(),\n",
    "    # 将图像的像素值归一化到[0,1]之间，0.1307和0.3081是MNIST数据集的均值和方差\n",
    "    transforms.Normalize((0.1307,), (0.3081,))\n",
    "])"
   ],
   "id": "318a87c01d84f02",
   "outputs": [],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-28T07:16:18.475874Z",
     "start_time": "2025-03-28T07:16:18.304704Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 加载MNIST数据集\n",
    "train_dataset = datasets.MNIST(root='./data', train=True, download=True, transform=transform)\n",
    "test_dataset = datasets.MNIST(root='./data', train=False, download=True, transform=transform)"
   ],
   "id": "fb12bebbf548dc84",
   "outputs": [],
   "execution_count": 14
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 计算训练集和测试集的均值和方差",
   "id": "8bc86d5f46676584"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-28T06:38:42.133254Z",
     "start_time": "2025-03-28T06:38:26.145579Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 设置数据加载器DataLoader， 顺便设置批次大小和是否打乱数据\n",
    "train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=60000, shuffle=True)\n",
    "test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=100, shuffle=False)\n",
    "\n",
    "for batch_idx, data in enumerate(train_loader, 0):\n",
    "    inputs, targets = data\n",
    "    # view函数会将训练集（60000,1，28，28）转化为（60000，784）\n",
    "    x = inputs.view(-1, 28 * 28)\n",
    "    x_std = x.std().item()\n",
    "    x_mean = x.mean().item()\n",
    "\n",
    "print(\"x_std:\" + str(x_std))\n",
    "print(\"x_mean:\" + str(x_mean))"
   ],
   "id": "e81e412ed203b63a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x_std:0.30810782313346863\n",
      "x_mean:0.13066047430038452\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-28T07:16:23.607743Z",
     "start_time": "2025-03-28T07:16:23.596058Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 设置数据加载器DataLoader， 顺便设置批次大小和是否打乱数据\n",
    "train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=128, shuffle=True)\n",
    "test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=1000, shuffle=False)"
   ],
   "id": "332bae7bac2588dc",
   "outputs": [],
   "execution_count": 15
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 通过自定义类来构建模型",
   "id": "936fd787c13483a1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-28T07:16:26.510317Z",
     "start_time": "2025-03-28T07:16:26.501813Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class Net(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(Net, self).__init__()\n",
    "        self.fc1 = nn.Linear(784, 128)\n",
    "        self.dropout = nn.Dropout(0.2)\n",
    "        self.fc2 = nn.Linear(128, 10)\n",
    "        \n",
    "    def forward(self, x):\n",
    "        x = torch.flatten(x, 1)\n",
    "        x = self.fc1(x)\n",
    "        x = F.relu(x)\n",
    "        x = self.dropout(x)\n",
    "        x = self.fc2(x)\n",
    "        output = F.log_softmax(x, dim=1)\n",
    "        return output"
   ],
   "id": "a2129a5930cf9787",
   "outputs": [],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 实例化模型，并将模型发送到device",
   "id": "22d3db3dfa504070"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-28T07:16:31.967677Z",
     "start_time": "2025-03-28T07:16:31.721252Z"
    }
   },
   "cell_type": "code",
   "source": "model = Net().to(device)",
   "id": "34ca85eacf3c4a93",
   "outputs": [],
   "execution_count": 17
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "4da73588b57ab26c"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 定义训练模型的逻辑",
   "id": "d5c14c71c96c50af"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-28T07:16:39.490812Z",
     "start_time": "2025-03-28T07:16:39.484113Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def train_step(data, target, model, optimizer):\n",
    "    optimizer.zero_grad()\n",
    "    output = model(data)\n",
    "    # nll_loss函数会计算输入和目标的负对数似然, nll是 negative log likelihood 的缩写--负对数似然\n",
    "    # 这里的target是one-hot编码的，所以需要使用nll_loss函数\n",
    "    loss = F.nll_loss(output, target)\n",
    "    # 反向传播的本质是计算梯度，并更新模型参数\n",
    "    loss.backward()\n",
    "    # 本质是应用梯度更新模型参数\n",
    "    optimizer.step()\n",
    "    return loss.item()"
   ],
   "id": "99dcbd210d8beada",
   "outputs": [],
   "execution_count": 18
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 定义测试模型的逻辑",
   "id": "8d37c2ce5afc52c6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-28T07:16:43.048743Z",
     "start_time": "2025-03-28T07:16:43.039751Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def test_step(data, target, model, test_loss, correct):\n",
    "    output = model(data)\n",
    "    # 计算累积的批次损失\n",
    "    test_loss += F.nll_loss(output, target, reduction='sum').item()\n",
    "    # 获得对数概率最大的下标， 这里其实是类别号\n",
    "    pred = output.argmax(dim=1, keepdim=True)  # dim=1表示按列取最大值，keepdim=True表示保持维度\n",
    "    correct += pred.eq(target.view_as(pred)).sum().item()\n",
    "    return test_loss, correct"
   ],
   "id": "43356d09a3af7d04",
   "outputs": [],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 设置训练调参使用的优化器",
   "id": "aa40f9ae10657ae2"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-28T07:16:45.756422Z",
     "start_time": "2025-03-28T07:16:45.740976Z"
    }
   },
   "cell_type": "code",
   "source": "optimizer = optim.Adam(model.parameters(), lr=0.001)",
   "id": "e21a4d35114542ec",
   "outputs": [],
   "execution_count": 20
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 训练模型",
   "id": "cd43fb71a681e69d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-28T07:23:30.359407Z",
     "start_time": "2025-03-28T07:19:29.174678Z"
    }
   },
   "cell_type": "code",
   "source": [
    "EPOCHS = 5\n",
    "\n",
    "for epoch in range(EPOCHS):\n",
    "    model.train()\n",
    "    for batch_idx, (data, target) in enumerate(train_loader):\n",
    "        data, target = data.to(device), target.to(device)\n",
    "        loss = train_step(data, target, model, optimizer)\n",
    "        # 每隔10个批次打印信息\n",
    "        if batch_idx % 10 == 0:\n",
    "            print('Train Epoch: {} [{}/{} ({:.0f}%)]\\tLoss: {:.6f}'.format(epoch, batch_idx * len(data), \n",
    "                len(train_loader.dataset),\n",
    "                100. * batch_idx / len(train_loader), loss))\n",
    "            \n",
    "    model.eval()\n",
    "    test_loss = 0\n",
    "    correct = 0\n",
    "    with torch.no_grad():\n",
    "        for data, target in test_loader:\n",
    "            data, target = data.to(device), target.to(device)\n",
    "            test_loss, correct = test_step(data, target, model, test_loss, correct)\n",
    "    test_loss /= len(test_loader.dataset)\n",
    "    print('\\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\\n'.format(\n",
    "        test_loss, correct, len(test_loader.dataset),\n",
    "        100. * correct / len(test_loader.dataset)))"
   ],
   "id": "aac227694c1d1ea8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train Epoch: 0 [0/60000 (0%)]\tLoss: 2.124089\n",
      "Train Epoch: 0 [1280/60000 (2%)]\tLoss: 1.052864\n",
      "Train Epoch: 0 [2560/60000 (4%)]\tLoss: 0.615869\n",
      "Train Epoch: 0 [3840/60000 (6%)]\tLoss: 0.416664\n",
      "Train Epoch: 0 [5120/60000 (9%)]\tLoss: 0.476797\n",
      "Train Epoch: 0 [6400/60000 (11%)]\tLoss: 0.406137\n",
      "Train Epoch: 0 [7680/60000 (13%)]\tLoss: 0.375986\n",
      "Train Epoch: 0 [8960/60000 (15%)]\tLoss: 0.373829\n",
      "Train Epoch: 0 [10240/60000 (17%)]\tLoss: 0.409779\n",
      "Train Epoch: 0 [11520/60000 (19%)]\tLoss: 0.289473\n",
      "Train Epoch: 0 [12800/60000 (21%)]\tLoss: 0.403253\n",
      "Train Epoch: 0 [14080/60000 (23%)]\tLoss: 0.442840\n",
      "Train Epoch: 0 [15360/60000 (26%)]\tLoss: 0.404302\n",
      "Train Epoch: 0 [16640/60000 (28%)]\tLoss: 0.447165\n",
      "Train Epoch: 0 [17920/60000 (30%)]\tLoss: 0.312061\n",
      "Train Epoch: 0 [19200/60000 (32%)]\tLoss: 0.255828\n",
      "Train Epoch: 0 [20480/60000 (34%)]\tLoss: 0.312906\n",
      "Train Epoch: 0 [21760/60000 (36%)]\tLoss: 0.244927\n",
      "Train Epoch: 0 [23040/60000 (38%)]\tLoss: 0.297773\n",
      "Train Epoch: 0 [24320/60000 (41%)]\tLoss: 0.385911\n",
      "Train Epoch: 0 [25600/60000 (43%)]\tLoss: 0.205903\n",
      "Train Epoch: 0 [26880/60000 (45%)]\tLoss: 0.327123\n",
      "Train Epoch: 0 [28160/60000 (47%)]\tLoss: 0.234726\n",
      "Train Epoch: 0 [29440/60000 (49%)]\tLoss: 0.137184\n",
      "Train Epoch: 0 [30720/60000 (51%)]\tLoss: 0.297301\n",
      "Train Epoch: 0 [32000/60000 (53%)]\tLoss: 0.226493\n",
      "Train Epoch: 0 [33280/60000 (55%)]\tLoss: 0.104972\n",
      "Train Epoch: 0 [34560/60000 (58%)]\tLoss: 0.223564\n",
      "Train Epoch: 0 [35840/60000 (60%)]\tLoss: 0.310174\n",
      "Train Epoch: 0 [37120/60000 (62%)]\tLoss: 0.220289\n",
      "Train Epoch: 0 [38400/60000 (64%)]\tLoss: 0.225690\n",
      "Train Epoch: 0 [39680/60000 (66%)]\tLoss: 0.212098\n",
      "Train Epoch: 0 [40960/60000 (68%)]\tLoss: 0.272749\n",
      "Train Epoch: 0 [42240/60000 (70%)]\tLoss: 0.222882\n",
      "Train Epoch: 0 [43520/60000 (72%)]\tLoss: 0.245514\n",
      "Train Epoch: 0 [44800/60000 (75%)]\tLoss: 0.290694\n",
      "Train Epoch: 0 [46080/60000 (77%)]\tLoss: 0.195710\n",
      "Train Epoch: 0 [47360/60000 (79%)]\tLoss: 0.165377\n",
      "Train Epoch: 0 [48640/60000 (81%)]\tLoss: 0.170637\n",
      "Train Epoch: 0 [49920/60000 (83%)]\tLoss: 0.228363\n",
      "Train Epoch: 0 [51200/60000 (85%)]\tLoss: 0.217923\n",
      "Train Epoch: 0 [52480/60000 (87%)]\tLoss: 0.221236\n",
      "Train Epoch: 0 [53760/60000 (90%)]\tLoss: 0.181653\n",
      "Train Epoch: 0 [55040/60000 (92%)]\tLoss: 0.268289\n",
      "Train Epoch: 0 [56320/60000 (94%)]\tLoss: 0.123512\n",
      "Train Epoch: 0 [57600/60000 (96%)]\tLoss: 0.167672\n",
      "Train Epoch: 0 [58880/60000 (98%)]\tLoss: 0.137489\n",
      "\n",
      "Test set: Average loss: 0.1606, Accuracy: 9535/10000 (95%)\n",
      "\n",
      "Train Epoch: 1 [0/60000 (0%)]\tLoss: 0.165579\n",
      "Train Epoch: 1 [1280/60000 (2%)]\tLoss: 0.261977\n",
      "Train Epoch: 1 [2560/60000 (4%)]\tLoss: 0.169964\n",
      "Train Epoch: 1 [3840/60000 (6%)]\tLoss: 0.216711\n",
      "Train Epoch: 1 [5120/60000 (9%)]\tLoss: 0.257250\n",
      "Train Epoch: 1 [6400/60000 (11%)]\tLoss: 0.198310\n",
      "Train Epoch: 1 [7680/60000 (13%)]\tLoss: 0.146192\n",
      "Train Epoch: 1 [8960/60000 (15%)]\tLoss: 0.186002\n",
      "Train Epoch: 1 [10240/60000 (17%)]\tLoss: 0.206461\n",
      "Train Epoch: 1 [11520/60000 (19%)]\tLoss: 0.224018\n",
      "Train Epoch: 1 [12800/60000 (21%)]\tLoss: 0.188573\n",
      "Train Epoch: 1 [14080/60000 (23%)]\tLoss: 0.159242\n",
      "Train Epoch: 1 [15360/60000 (26%)]\tLoss: 0.209314\n",
      "Train Epoch: 1 [16640/60000 (28%)]\tLoss: 0.269730\n",
      "Train Epoch: 1 [17920/60000 (30%)]\tLoss: 0.132256\n",
      "Train Epoch: 1 [19200/60000 (32%)]\tLoss: 0.130637\n",
      "Train Epoch: 1 [20480/60000 (34%)]\tLoss: 0.116632\n",
      "Train Epoch: 1 [21760/60000 (36%)]\tLoss: 0.276809\n",
      "Train Epoch: 1 [23040/60000 (38%)]\tLoss: 0.148314\n",
      "Train Epoch: 1 [24320/60000 (41%)]\tLoss: 0.196394\n",
      "Train Epoch: 1 [25600/60000 (43%)]\tLoss: 0.225838\n",
      "Train Epoch: 1 [26880/60000 (45%)]\tLoss: 0.319678\n",
      "Train Epoch: 1 [28160/60000 (47%)]\tLoss: 0.131384\n",
      "Train Epoch: 1 [29440/60000 (49%)]\tLoss: 0.120971\n",
      "Train Epoch: 1 [30720/60000 (51%)]\tLoss: 0.220639\n",
      "Train Epoch: 1 [32000/60000 (53%)]\tLoss: 0.088368\n",
      "Train Epoch: 1 [33280/60000 (55%)]\tLoss: 0.059179\n",
      "Train Epoch: 1 [34560/60000 (58%)]\tLoss: 0.177705\n",
      "Train Epoch: 1 [35840/60000 (60%)]\tLoss: 0.087120\n",
      "Train Epoch: 1 [37120/60000 (62%)]\tLoss: 0.054357\n",
      "Train Epoch: 1 [38400/60000 (64%)]\tLoss: 0.160322\n",
      "Train Epoch: 1 [39680/60000 (66%)]\tLoss: 0.339439\n",
      "Train Epoch: 1 [40960/60000 (68%)]\tLoss: 0.096511\n",
      "Train Epoch: 1 [42240/60000 (70%)]\tLoss: 0.084751\n",
      "Train Epoch: 1 [43520/60000 (72%)]\tLoss: 0.138666\n",
      "Train Epoch: 1 [44800/60000 (75%)]\tLoss: 0.196646\n",
      "Train Epoch: 1 [46080/60000 (77%)]\tLoss: 0.153251\n",
      "Train Epoch: 1 [47360/60000 (79%)]\tLoss: 0.137951\n",
      "Train Epoch: 1 [48640/60000 (81%)]\tLoss: 0.098614\n",
      "Train Epoch: 1 [49920/60000 (83%)]\tLoss: 0.148312\n",
      "Train Epoch: 1 [51200/60000 (85%)]\tLoss: 0.112873\n",
      "Train Epoch: 1 [52480/60000 (87%)]\tLoss: 0.284614\n",
      "Train Epoch: 1 [53760/60000 (90%)]\tLoss: 0.127430\n",
      "Train Epoch: 1 [55040/60000 (92%)]\tLoss: 0.083681\n",
      "Train Epoch: 1 [56320/60000 (94%)]\tLoss: 0.204976\n",
      "Train Epoch: 1 [57600/60000 (96%)]\tLoss: 0.077899\n",
      "Train Epoch: 1 [58880/60000 (98%)]\tLoss: 0.132999\n",
      "\n",
      "Test set: Average loss: 0.1140, Accuracy: 9672/10000 (97%)\n",
      "\n",
      "Train Epoch: 2 [0/60000 (0%)]\tLoss: 0.137731\n",
      "Train Epoch: 2 [1280/60000 (2%)]\tLoss: 0.157151\n",
      "Train Epoch: 2 [2560/60000 (4%)]\tLoss: 0.165354\n",
      "Train Epoch: 2 [3840/60000 (6%)]\tLoss: 0.053154\n",
      "Train Epoch: 2 [5120/60000 (9%)]\tLoss: 0.126690\n",
      "Train Epoch: 2 [6400/60000 (11%)]\tLoss: 0.087929\n",
      "Train Epoch: 2 [7680/60000 (13%)]\tLoss: 0.107716\n",
      "Train Epoch: 2 [8960/60000 (15%)]\tLoss: 0.117820\n",
      "Train Epoch: 2 [10240/60000 (17%)]\tLoss: 0.076662\n",
      "Train Epoch: 2 [11520/60000 (19%)]\tLoss: 0.082278\n",
      "Train Epoch: 2 [12800/60000 (21%)]\tLoss: 0.082325\n",
      "Train Epoch: 2 [14080/60000 (23%)]\tLoss: 0.130518\n",
      "Train Epoch: 2 [15360/60000 (26%)]\tLoss: 0.180920\n",
      "Train Epoch: 2 [16640/60000 (28%)]\tLoss: 0.123218\n",
      "Train Epoch: 2 [17920/60000 (30%)]\tLoss: 0.078846\n",
      "Train Epoch: 2 [19200/60000 (32%)]\tLoss: 0.200989\n",
      "Train Epoch: 2 [20480/60000 (34%)]\tLoss: 0.135554\n",
      "Train Epoch: 2 [21760/60000 (36%)]\tLoss: 0.096120\n",
      "Train Epoch: 2 [23040/60000 (38%)]\tLoss: 0.134756\n",
      "Train Epoch: 2 [24320/60000 (41%)]\tLoss: 0.116480\n",
      "Train Epoch: 2 [25600/60000 (43%)]\tLoss: 0.077354\n",
      "Train Epoch: 2 [26880/60000 (45%)]\tLoss: 0.124975\n",
      "Train Epoch: 2 [28160/60000 (47%)]\tLoss: 0.163722\n",
      "Train Epoch: 2 [29440/60000 (49%)]\tLoss: 0.092450\n",
      "Train Epoch: 2 [30720/60000 (51%)]\tLoss: 0.108002\n",
      "Train Epoch: 2 [32000/60000 (53%)]\tLoss: 0.109432\n",
      "Train Epoch: 2 [33280/60000 (55%)]\tLoss: 0.063200\n",
      "Train Epoch: 2 [34560/60000 (58%)]\tLoss: 0.174489\n",
      "Train Epoch: 2 [35840/60000 (60%)]\tLoss: 0.069665\n",
      "Train Epoch: 2 [37120/60000 (62%)]\tLoss: 0.080446\n",
      "Train Epoch: 2 [38400/60000 (64%)]\tLoss: 0.089871\n",
      "Train Epoch: 2 [39680/60000 (66%)]\tLoss: 0.161782\n",
      "Train Epoch: 2 [40960/60000 (68%)]\tLoss: 0.160403\n",
      "Train Epoch: 2 [42240/60000 (70%)]\tLoss: 0.065807\n",
      "Train Epoch: 2 [43520/60000 (72%)]\tLoss: 0.088416\n",
      "Train Epoch: 2 [44800/60000 (75%)]\tLoss: 0.134729\n",
      "Train Epoch: 2 [46080/60000 (77%)]\tLoss: 0.077648\n",
      "Train Epoch: 2 [47360/60000 (79%)]\tLoss: 0.179923\n",
      "Train Epoch: 2 [48640/60000 (81%)]\tLoss: 0.106700\n",
      "Train Epoch: 2 [49920/60000 (83%)]\tLoss: 0.110146\n",
      "Train Epoch: 2 [51200/60000 (85%)]\tLoss: 0.051979\n",
      "Train Epoch: 2 [52480/60000 (87%)]\tLoss: 0.081755\n",
      "Train Epoch: 2 [53760/60000 (90%)]\tLoss: 0.167849\n",
      "Train Epoch: 2 [55040/60000 (92%)]\tLoss: 0.124906\n",
      "Train Epoch: 2 [56320/60000 (94%)]\tLoss: 0.148652\n",
      "Train Epoch: 2 [57600/60000 (96%)]\tLoss: 0.126725\n",
      "Train Epoch: 2 [58880/60000 (98%)]\tLoss: 0.095309\n",
      "\n",
      "Test set: Average loss: 0.0951, Accuracy: 9724/10000 (97%)\n",
      "\n",
      "Train Epoch: 3 [0/60000 (0%)]\tLoss: 0.081477\n",
      "Train Epoch: 3 [1280/60000 (2%)]\tLoss: 0.283338\n",
      "Train Epoch: 3 [2560/60000 (4%)]\tLoss: 0.066096\n",
      "Train Epoch: 3 [3840/60000 (6%)]\tLoss: 0.074580\n",
      "Train Epoch: 3 [5120/60000 (9%)]\tLoss: 0.120565\n",
      "Train Epoch: 3 [6400/60000 (11%)]\tLoss: 0.145699\n",
      "Train Epoch: 3 [7680/60000 (13%)]\tLoss: 0.092848\n",
      "Train Epoch: 3 [8960/60000 (15%)]\tLoss: 0.095303\n",
      "Train Epoch: 3 [10240/60000 (17%)]\tLoss: 0.038285\n",
      "Train Epoch: 3 [11520/60000 (19%)]\tLoss: 0.105935\n",
      "Train Epoch: 3 [12800/60000 (21%)]\tLoss: 0.076010\n",
      "Train Epoch: 3 [14080/60000 (23%)]\tLoss: 0.126496\n",
      "Train Epoch: 3 [15360/60000 (26%)]\tLoss: 0.097242\n",
      "Train Epoch: 3 [16640/60000 (28%)]\tLoss: 0.158218\n",
      "Train Epoch: 3 [17920/60000 (30%)]\tLoss: 0.122740\n",
      "Train Epoch: 3 [19200/60000 (32%)]\tLoss: 0.147397\n",
      "Train Epoch: 3 [20480/60000 (34%)]\tLoss: 0.062449\n",
      "Train Epoch: 3 [21760/60000 (36%)]\tLoss: 0.089926\n",
      "Train Epoch: 3 [23040/60000 (38%)]\tLoss: 0.123268\n",
      "Train Epoch: 3 [24320/60000 (41%)]\tLoss: 0.093625\n",
      "Train Epoch: 3 [25600/60000 (43%)]\tLoss: 0.112392\n",
      "Train Epoch: 3 [26880/60000 (45%)]\tLoss: 0.059229\n",
      "Train Epoch: 3 [28160/60000 (47%)]\tLoss: 0.039143\n",
      "Train Epoch: 3 [29440/60000 (49%)]\tLoss: 0.122321\n",
      "Train Epoch: 3 [30720/60000 (51%)]\tLoss: 0.196234\n",
      "Train Epoch: 3 [32000/60000 (53%)]\tLoss: 0.206271\n",
      "Train Epoch: 3 [33280/60000 (55%)]\tLoss: 0.140347\n",
      "Train Epoch: 3 [34560/60000 (58%)]\tLoss: 0.152270\n",
      "Train Epoch: 3 [35840/60000 (60%)]\tLoss: 0.084580\n",
      "Train Epoch: 3 [37120/60000 (62%)]\tLoss: 0.053772\n",
      "Train Epoch: 3 [38400/60000 (64%)]\tLoss: 0.064000\n",
      "Train Epoch: 3 [39680/60000 (66%)]\tLoss: 0.045311\n",
      "Train Epoch: 3 [40960/60000 (68%)]\tLoss: 0.127479\n",
      "Train Epoch: 3 [42240/60000 (70%)]\tLoss: 0.072736\n",
      "Train Epoch: 3 [43520/60000 (72%)]\tLoss: 0.035114\n",
      "Train Epoch: 3 [44800/60000 (75%)]\tLoss: 0.154575\n",
      "Train Epoch: 3 [46080/60000 (77%)]\tLoss: 0.080612\n",
      "Train Epoch: 3 [47360/60000 (79%)]\tLoss: 0.083766\n",
      "Train Epoch: 3 [48640/60000 (81%)]\tLoss: 0.083822\n",
      "Train Epoch: 3 [49920/60000 (83%)]\tLoss: 0.100438\n",
      "Train Epoch: 3 [51200/60000 (85%)]\tLoss: 0.075132\n",
      "Train Epoch: 3 [52480/60000 (87%)]\tLoss: 0.124882\n",
      "Train Epoch: 3 [53760/60000 (90%)]\tLoss: 0.118463\n",
      "Train Epoch: 3 [55040/60000 (92%)]\tLoss: 0.080427\n",
      "Train Epoch: 3 [56320/60000 (94%)]\tLoss: 0.116182\n",
      "Train Epoch: 3 [57600/60000 (96%)]\tLoss: 0.124319\n",
      "Train Epoch: 3 [58880/60000 (98%)]\tLoss: 0.037175\n",
      "\n",
      "Test set: Average loss: 0.0923, Accuracy: 9718/10000 (97%)\n",
      "\n",
      "Train Epoch: 4 [0/60000 (0%)]\tLoss: 0.066011\n",
      "Train Epoch: 4 [1280/60000 (2%)]\tLoss: 0.176090\n",
      "Train Epoch: 4 [2560/60000 (4%)]\tLoss: 0.063603\n",
      "Train Epoch: 4 [3840/60000 (6%)]\tLoss: 0.059262\n",
      "Train Epoch: 4 [5120/60000 (9%)]\tLoss: 0.053669\n",
      "Train Epoch: 4 [6400/60000 (11%)]\tLoss: 0.070961\n",
      "Train Epoch: 4 [7680/60000 (13%)]\tLoss: 0.150267\n",
      "Train Epoch: 4 [8960/60000 (15%)]\tLoss: 0.093381\n",
      "Train Epoch: 4 [10240/60000 (17%)]\tLoss: 0.101154\n",
      "Train Epoch: 4 [11520/60000 (19%)]\tLoss: 0.046632\n",
      "Train Epoch: 4 [12800/60000 (21%)]\tLoss: 0.068927\n",
      "Train Epoch: 4 [14080/60000 (23%)]\tLoss: 0.122762\n",
      "Train Epoch: 4 [15360/60000 (26%)]\tLoss: 0.063932\n",
      "Train Epoch: 4 [16640/60000 (28%)]\tLoss: 0.092909\n",
      "Train Epoch: 4 [17920/60000 (30%)]\tLoss: 0.098373\n",
      "Train Epoch: 4 [19200/60000 (32%)]\tLoss: 0.120561\n",
      "Train Epoch: 4 [20480/60000 (34%)]\tLoss: 0.026482\n",
      "Train Epoch: 4 [21760/60000 (36%)]\tLoss: 0.092348\n",
      "Train Epoch: 4 [23040/60000 (38%)]\tLoss: 0.081622\n",
      "Train Epoch: 4 [24320/60000 (41%)]\tLoss: 0.062754\n",
      "Train Epoch: 4 [25600/60000 (43%)]\tLoss: 0.114334\n",
      "Train Epoch: 4 [26880/60000 (45%)]\tLoss: 0.076728\n",
      "Train Epoch: 4 [28160/60000 (47%)]\tLoss: 0.074101\n",
      "Train Epoch: 4 [29440/60000 (49%)]\tLoss: 0.103777\n",
      "Train Epoch: 4 [30720/60000 (51%)]\tLoss: 0.142885\n",
      "Train Epoch: 4 [32000/60000 (53%)]\tLoss: 0.054331\n",
      "Train Epoch: 4 [33280/60000 (55%)]\tLoss: 0.060424\n",
      "Train Epoch: 4 [34560/60000 (58%)]\tLoss: 0.108599\n",
      "Train Epoch: 4 [35840/60000 (60%)]\tLoss: 0.052234\n",
      "Train Epoch: 4 [37120/60000 (62%)]\tLoss: 0.125702\n",
      "Train Epoch: 4 [38400/60000 (64%)]\tLoss: 0.064044\n",
      "Train Epoch: 4 [39680/60000 (66%)]\tLoss: 0.105146\n",
      "Train Epoch: 4 [40960/60000 (68%)]\tLoss: 0.093911\n",
      "Train Epoch: 4 [42240/60000 (70%)]\tLoss: 0.069772\n",
      "Train Epoch: 4 [43520/60000 (72%)]\tLoss: 0.090436\n",
      "Train Epoch: 4 [44800/60000 (75%)]\tLoss: 0.040139\n",
      "Train Epoch: 4 [46080/60000 (77%)]\tLoss: 0.033578\n",
      "Train Epoch: 4 [47360/60000 (79%)]\tLoss: 0.031999\n",
      "Train Epoch: 4 [48640/60000 (81%)]\tLoss: 0.169342\n",
      "Train Epoch: 4 [49920/60000 (83%)]\tLoss: 0.174974\n",
      "Train Epoch: 4 [51200/60000 (85%)]\tLoss: 0.069819\n",
      "Train Epoch: 4 [52480/60000 (87%)]\tLoss: 0.084908\n",
      "Train Epoch: 4 [53760/60000 (90%)]\tLoss: 0.162924\n",
      "Train Epoch: 4 [55040/60000 (92%)]\tLoss: 0.069854\n",
      "Train Epoch: 4 [56320/60000 (94%)]\tLoss: 0.033408\n",
      "Train Epoch: 4 [57600/60000 (96%)]\tLoss: 0.208229\n",
      "Train Epoch: 4 [58880/60000 (98%)]\tLoss: 0.044496\n",
      "\n",
      "Test set: Average loss: 0.0839, Accuracy: 9761/10000 (98%)\n",
      "\n"
     ]
    }
   ],
   "execution_count": 22
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
